<?php
/**
 * @author sunqiang
 */
namespace App\Traits;
use Illuminate\Http\Request;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use Illuminate\Support\Facades\Log;
//公共函数
trait BaseFun
{
	public static function getcposition($ip){
        // $res1 = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$ip");
        // $res1 = json_decode($res1,true);
        // if ($res1[ "code"]==0){
        //     if($ip=="127.0.0.1"){
        //         return "内网";
        //     }
        //     return $res1['data']["country"].$res1['data'][ "region"].$res1['data']["city"]."_".$res1['data'][ "isp"];
        // }else{
            return "未知";
        // } 
    }
    public static function jsonReturn($arr=[]){
        //为0正常,其他需要输入错误信息$state,$date,$token,$msg=''
        if(!$arr['state']){
            return ['state'=>$arr['state'],'data'=>$arr['data'],'token'=>$arr['token']];
        }
        return ['state'=>$arr['state'],'data'=>$arr['data'],'msg'=>$arr['msg']];
    }

    public static function sendSms($mobile,$code){
        //初始化配置
        AlibabaCloud::accessKeyClient('LTAI4FjMkgzndbQgkooyVJK2', '0eHmQC8VnlsRCVr9Owvx54xS0IRUET')
                        ->regionId('cn-hangzhou')
                        ->asDefaultClient();
                try {
                    $result = AlibabaCloud::rpc()
                                          ->product('Dysmsapi')
                                          // ->scheme('https') // https | http
                                          ->version('2017-05-25')
                                          ->action('SendSms')
                                          ->method('POST')
                                          ->host('dysmsapi.aliyuncs.com')
                                          ->options([
                                                        'query' => [
                                                          'RegionId' => "cn-hangzhou",
                                                          'PhoneNumbers' => $mobile,
                                                          'SignName' => "沧州有道鲜生",
                                                          'TemplateCode' => "SMS_177548659",
                                                          'TemplateParam' => json_encode(['code'=>$code]),
                                                        ],
                                                    ])
                                          ->request();
                    // print_r($result->toArray());
                    return true;
                } catch (ClientException $e) {
                    echo $e->getErrorMessage() . PHP_EOL;
                } catch (ServerException $e) {
                    echo $e->getErrorMessage() . PHP_EOL;
                }
    }


} 